package com.adv.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.adv.adapter.DetailAdapter;
import com.adv.object.DetailObj;

public class Details extends SherlockActivity {

	protected static final String LINK_DETAIL = "keylink";
	private List<DetailObj> objects;
	private ActionBar actionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setSubtitle("Danh sách khuyến mãi");

		objects = new ArrayList<DetailObj>();

		// TODO: retrive distributor_id to query DB
		Intent intent = getIntent();
		int d_ID = intent.getIntExtra(Distributors.DISTRIBUTOR_ID_KEY, -1);
		if (d_ID != -1) {
			Log.e(getPackageName(), "Details Activity: distributor ID " + d_ID);
		} else {
			Log.e(getPackageName(), "Details Activity: distributor ID " + d_ID);
		}

		// image sale test data
		//TODO: set value from json
		objects.add(new DetailObj(
				1,
				"Title",
				"http://pico.vn/black-friday--thu-sau-den-toi-duy-nhat-mot-ngay-2712-tai-picovn-m.70.html",
				"2013-12-10 05:15:00", 1));
		objects.add(new DetailObj(
				1,
				"Title",
				"http://pico.vn/black-friday--thu-sau-den-toi-duy-nhat-mot-ngay-2712-tai-picovn-m.70.html",
				"2013-12-10 05:15:00", 1));
		objects.add(new DetailObj(
				1,
				"Title",
				"http://pico.vn/black-friday--thu-sau-den-toi-duy-nhat-mot-ngay-2712-tai-picovn-m.70.html",
				"2013-12-10 05:15:00", 1));

		if (objects.size() == 1) {
			// put link -> Fullscreen to load (case: 1 child)
			Intent i = new Intent(Details.this, Fullscreen.class);
			i.putExtra(LINK_DETAIL, objects.get(0).getLink());

			Log.e(getPackageName(), "Detail send link:"
					+ objects.get(0).getLink());

			startActivity(i);
		}

		ListView lDetails = (ListView) findViewById(R.id.list_detail);

		//  IconDistributor path from Distributor activity
		String iconDistributor = getIntent().getStringExtra(
				Distributors.DISTRIBUTOR_ICON_CACHE);
		Log.e(getPackageName(), "Details acti: get icon cache: "
				+ iconDistributor);
		lDetails.setAdapter(new DetailAdapter(this, R.layout.detail_item,
				objects, iconDistributor));

		lDetails.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View v, int pos,
					long id) {
				// put link -> Fullscreen to load
				Intent i = new Intent(Details.this, Fullscreen.class);
				i.putExtra(LINK_DETAIL, objects.get(pos).getLink());

				Log.e(getPackageName(), "Detail send link:"
						+ objects.get(pos).getLink());

				startActivity(i);

			}
		});

	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId,
			com.actionbarsherlock.view.MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
		} else if (item.getItemId() == R.id.action_home) {
			startActivity(new Intent(Details.this, Catalog.class));
			finish();
		}
		return true;
	}
}
